<?php

namespace App\Http\Controllers\Store;

use App\Model\Department;
use App\Model\Leave;
use App\Model\Move_task;
use App\Model\Role;
use App\Model\Staff;
use App\Model\Staff_task;
use App\Model\Store;
use App\Model\Store_station;
use App\Model\Task;
use App\Model\Workflow;
use App\Model\Workflow_desc;
use App\Model\Workflow_group;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;

class TaskController extends CommonController
{
    //


	protected $task;
	protected $store_station;
	protected $department;
	protected $staff_task;
	protected $leave;
	protected $staff;
	protected $store;
	protected $Move_task;
	protected $Workflow;
	protected $Workflow_desc;
	protected static $uid;
	protected $Role;
	protected static $userInfo;
	protected $Workflow_group;
	public function __construct()
	{
		parent::__construct();

		$this->task=new Task();
		$this->store_station=new Store_station();
		$this->department=new Department();
		$this->staff_task=new Staff_task();
		$this->staff=new Staff();
		$this->leave=new Leave();
		$this->store=new Store();
		$this->Move_task=new Move_task();
		$this->Workflow=new Workflow();
		$this->Workflow_desc=new Workflow_desc();
		$this->Role=new Role();
		self::$userInfo=Auth::user();
		self::$uid=Auth::user()->id;
		$this->Workflow_group=new Workflow_group();

	}




//	任务列表
	public function task_list(Request $request){
		$taskData=$this->task->task_list($this->store_id);
		$move_taskData=$this->Move_task->no_lists($this->store_id);
		$newArr=[];$str=[];
//		return $taskData;
		foreach($taskData as $kk=>$vv){
//			$newArr[$kk]['id']=$vv->id;
			if($vv['type']==1){
				$newArr[$kk]['id']=$vv['id'];
				$newArr[$kk]['type']=1;
				$newArr[$kk]['name']=$vv['store_station']['name'];
				if($vv['store_station']['status']==1){
					$newArr[$kk]['status']='永久有效';
				}
				if($vv['store_station']['status']==0){
					$newArr[$kk]['status']='短期有效';
				}
				if($vv['store_station']['status']==-1){
					$newArr[$kk]['status']='失效';
				}
				$newArr[$kk]['cycle_start']=$vv['store_station']['start_time'];

				$newArr[$kk]['cycle_end']=$vv['store_station']['end_time'];

				$newArr[$kk]['num']=$vv['num'];
				$newArr[$kk]['to_department']=null;
				$newArr[$kk]['start_time']=null;
				$newArr[$kk]['end_time']=null;
				$newArr[$kk]['work_start']=null;
				$newArr[$kk]['work_end']=null;
			}

		}

		foreach ($move_taskData as $k=>$v){
				$str[$k]['id']=$v['id'];
				$str[$k]['type']=2;
				$str[$k]['name']=$v['store_station']['name'];
				$str[$k]['status']=null;
				$str[$k]['cycle_start']=null;
				$str[$k]['cycle_end']=null;
				$str[$k]['num']=null;
				$str[$k]['to_department']=$v['store']['name'];
				$str[$k]['start_day']=$v['start_day'];
				$str[$k]['end_day']=$v['start_day'];
				$str[$k]['work_start']=$v['start_time'];
				$str[$k]['work_end']=$v['end_time'];

		}
		$Arr=array_merge($newArr,$str);

		$data['data']=$Arr;
		$data['worki_hours']=400;
		$data['completed']=20;
		return $this->success($data);
	}


//	创建新任务
	public function add_task(){
		$store_name=$this->store->where(['id'=>$this->store_id])->first()->name;
		$data['store_id']=$this->store_id;
		$data['store_name']=$store_name;
		$data['move_task']['start_day']=null;
		$data['move_task']['end_day']=null;
		$data['move_task']['start_time']=null;
		$data['move_task']['end_time']=null;
		return $this->success($data);
	}
//	获取所有店铺岗位
	public function allStation(){
		$data= $this->store_station->allStations($this->store_id);
		$newArr=[];
		foreach($data as $k=>$v){
			$newArr[$k]['id']=$v['id'];
			$newArr[$k]['name']=$v['name'];
		}
		array_push($newArr,['id'=>null,'name'=>'全部']);
		return $this->success($newArr);
	}

//	店长端获取所有店铺岗位
	public function store_Station(){
		$data= $this->store_station->where(['store_id'=>$this->store_id,'status'=>1])->get();
		return $this->success($data);
	}


	//获取所有店铺岗位详情
	public function station_desc(Request $request){
		$data= $this->store_station->store_station_desc($request->id);
		if($data['status']==0){
			$data['statu']='短期有效';
		}
		if($data['status']==1){
			$data['statu']='长期有效';
		}
		if($data['status']==-1){
			$data['statu']='失效';
		}
		return $this->success($data);
	}

	//获取所有的店铺
	public function all_department(){
		$data=$this->store->all_depart($this->company_id);
		return $this->success($data);
	}

//	获取该店铺下所有的岗位
	public function  all_store_station(Request $request){
		$data=$this->store_station->allStations($request->id);
		$newArr=[];
		foreach($data  as $k=>$v){
			$newArr[$k]['id']=$v['id'];
			$newArr[$k]['name']=$v['name'];
		}
		return $this->success($newArr);
	}

//	保存任务

	public function save_task(Request $request){

		$this->validate($request,[
			'type' => 'required|Integer',
			'store_station_id'=>'Integer',
			'num' => 'Integer',
			'start_day' => 'date',
			'status' => 'Integer',
			'end_day'=>'date',
			'start'=>'date',
			'end'=>'date',
			'to_store_id' => 'Integer',
			'to_store_station_id' => 'Integer',
		]);
//		模拟调动任务数据
//		$data['type']=$request->type=2;
//		$data['to_store_id']=2;
//		$data['to_store_station_id']=1;
//		$data['start_day']='2018-06-16';
//		$data['end_day']='2018-06-18';
//		$data['start_time']='08:00:00';
//		$data['end_time']='18:00:00';
//		$data['status']=1;
//		$data['comment']=2;
//		模拟调动任务数据结束
//		添加普通任务
		if($request->type==1){
			$data['store_id']=$this->store_id;
			$data['type']=$request->type;
			$data['store_station_id']=$request->store_station_id;
			$data['num']=$request->num;
			$data['start_day']=$request->start;
			$data['end_day']=$request->end;
			$data['status']=$request->status;
			$data['comment']=$request->task_comment;
		}
//		添加调动任务
		if($request->type==2){
			$data['to_store_id']=$this->store_id;//调入部门id
			$data['type']=$request->type;
			$data['status']=$request->status;
			$data['to_store_station_id']=$request->to_store_station_id;//支援岗位id
			$data['start_day']=$request->start_day;//支援开始日期
			$data['end_day']=$request->end_day;//支援结束日期
			$data['start_time']=$request->start_time;//支援开始时间
			$data['end_time']=$request->end_time;//支援结束时间
			$data['comment']=$request->comment;//任务描述
		}
		DB::beginTransaction();
		try{
			if($request->type==1){
				$result=$this->task->create($data);
			}elseif ($request->type==2){
				$result=$this->Move_task->create($data);
				$id=$result->id;
				$this->binding($id);
			}
			DB::commit();
		}catch (\Exception $exception){
			DB::rollBack();
			$message=$exception->getMessage();
		}

		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}



//	编辑任务
	public function task_desc(Request $request){
		$type=$request->type;
		$desc=$this->task->task_desc($request->id);
		$newArr=[];
//			$newArr['type']=$desc['type'];  //任务类型
//			$newArr['store_station_id']=$desc['store_station_id']; //店铺岗位id
//			$newArr['name']=$desc['store_station']['name'];  //店铺岗位名称
//			$newArr['start_day']=$desc['start_day'];  //开始时间
//			$newArr['end_day']=$desc['end_day'];  //结束时间
//			$newArr['num']=$desc['num']; //需要人数
//			$newArr['comment']=$desc['comment'];  //任务描述
//
//			$newArr['to_department_id']=$desc['to_department_id'];
//			$newArr['to_store_station_id']=$desc['to_store_station_id'];
//			$newArr['start_time']=$desc['start_time'];
//			$newArr['end_time']=$desc['end_time'];
//			$newArr['store_station']['status']=is_null($desc['store_station'])?null:$desc['store_station']['status']; //岗位有效状态:0短期有效，1永久有效,-1失效
//			$newArr['station']['standard_hour']=is_null($desc['store_station'])?null:$desc['store_station']['station']['standard_hour'];   //标准工时
//			$newArr['station']['standard_day']=is_null($desc['store_station'])?null:$desc['store_station']['station']['standard_day'];  //月工作天数
//			$newArr['station']['hour_salary']=is_null($desc['store_station'])?null:$desc['store_station']['station']['hour_salary'];  //岗位时薪


		if($type==1){
			$desc=$this->task->task_desc($request->id);
			$newArr['type']=$desc['type'];  //任务类型
			$newArr['store_station_id']=$desc['store_station_id']; //店铺岗位id
			$newArr['name']=$desc['store_station']['name'];  //店铺岗位名称
			$newArr['start_day']=$desc['start_day'];  //开始时间
			$newArr['end_day']=$desc['end_day'];  //结束时间
			$newArr['num']=$desc['num']; //需要人数
			$newArr['comment']=$desc['comment'];  //任务描述
			$newArr['store_station']['status']=is_null($desc['store_station'])?null:$desc['store_station']['status']; //岗位有效状态:0短期有效，1永久有效,-1失效
			$newArr['station']['standard_hour']=is_null($desc['store_station'])?null:$desc['store_station']['station']['standard_hour'];   //标准工时
			$newArr['station']['standard_day']=is_null($desc['store_station'])?null:$desc['store_station']['station']['standard_day'];  //月工作天数
			$newArr['station']['hour_salary']=is_null($desc['store_station'])?null:$desc['store_station']['station']['hour_salary'];  //岗位时
		}
		if($type==2){
			$desc=$this->Move_task->task_desc($request->id);
			$newArr['type']=$desc['type'];  //任务类型
			$newArr['store']['id']=$desc['store']['id'];  //调入部门id
			$newArr['store']['name']=$desc['store']['name'];  //调入部门名称
			$newArr['store_station_']['id']=$desc['store_station']['id'];  //支援岗位id
			$newArr['store_station_']['name']=$desc['store_station']['name'];  //支援岗位名称
			$newArr['start_day']=$desc['start_day'];//开始日期
			$newArr['end_day']=$desc['end_day'];//结束日期
			$newArr['start_time']=$desc['start_time'];//开始时间
			$newArr['end_time']=$desc['end_time'];//结束时间
			$newArr['comment']=$desc['comment'];  //任务描述
		}
		return $this->success($newArr);

	}
//	编辑保存任务
	public function update_task(Request $request){
		$this->validate($request,[
			'id'=>'required|Integer',
			'type' => 'required|Integer',
			'store_station_id'=>'Integer',
			'num' => 'Integer',
			'status' => 'Integer',
			'start_day' => 'date',
			'end_day'=>'date',
			'start'=>'date',
			'end'=>'date',
			'to_store_id' => 'Integer',
			'to_store_station_id' => 'Integer',
		]);
		if($request->type==1){
			$data['id']=$request->id;
			$data['store_id']=$this->store_id;
			$data['type']=$request->type;
			$data['store_station_id']=$request->store_station_id;
			$data['num']=$request->num;
			$data['start_day']=$request->start;
			$data['end_day']=$request->end;
			$data['status']=$request->status;
			$data['comment']=$request->task_comment;
		}
//		添加调动任务
		if($request->type==2){
			$array['id']=$request->id;
			$array['to_store_id']=$this->store_id;//调入部门id
			$array['type']=$request->type;
			$array['status']=$request->status;
			$array['to_store_station_id']=$request->to_store_station_id;//支援岗位id
			$array['start_day']=$request->start_day;//支援开始日期
			$array['end_day']=$request->end_day;//支援结束日期
			$array['start_time']=$request->start_time;//支援开始时间
			$array['end_time']=$request->end_time;//支援结束时间
			$array['comment']=$request->comment;//任务描述
		}
		DB::beginTransaction();
		try{
			if($data['type']==1){
				$result=$this->task->update_desc($data);
			}
			if($data['type']==2){
				$result=$this->Move_task->update_desc($array);
			}
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}

	}

//	删除任务
	public function del_task(Request $request){
		$id=$request->input();
		try{
			$result=$this->task->whereIn('id',$id)->delete();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return ['code'=>'204','message'=>'删除成功','data'=>$result];
		}else{
			return $this->failed($message);
		}
	}

//	接单安排列表
	public function receipt_list(Request $request){
		$store_id=$this->store_id;
		$data=$this->task->receipt_list($store_id);
		foreach($data as $kk=>$vv){
			if($vv['store_station']['status']==0){
				$vv['statu']='短期有效';
			}
			if($vv['store_station']['status']==1){
				$vv['statu']='长期有效';
			}
			if($vv['store_station']['status']==-1){
				$vv['statu']='失效';
			}
		}
		return $this->success($data);
	}

//	接单详情
	public function receipt_desc($id){
		$data=$this->task->desc($id);
		$result['station_name']=$data->store_station->station->name;
		$result['status']=$data['store_station']['station']['status']==1?'永久有效':'短期有效';
		$result['start_time']=$data->store_station->start_time;
		$result['end_time']=$data->store_station->end_time;
		$result['num']=$data->store_station->num;
		$result['comment']=$data['store_station']['station']['comment'];
		return $this->success($result);
	}

//	接单执行人列表
	public function implement_list(Request $request){
		$store_id=$this->store_id;
		$task_id=$request->task_id;
		try{
			$data['data']=$this->staff_task->allimplement($store_id,$task_id);
			$data['num']=$this->task->where(['id'=>$task_id])->first()->num;
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}

//	派单到个人
	public function dispatchs(Request $request){
		$data['task_id']=$request->task_id;
		$data['store_station_id']=Task::where(['id'=>$data['task_id']])->first()->store_station_id;
		$data['staff_id']=$request->staff_id;
//		模拟数据
//		$data['staff_id']=[1,2];
//		模拟数据结束
		$num=count($data['staff_id']);
		DB::beginTransaction();
		try{
			$this->staff->staff_station($this->store_id,$data['staff_id'],$data['store_station_id']);
			DB::table('staff_task')->where(['task_id'=>$data['task_id']])->update(['status'=>2]);
			$result=DB::table('staff_task')->where(['task_id'=>$data['task_id']])
				->whereIn('staff_id', $data['staff_id'])->update(['status'=>3]);
			$this->task->where(['id'=>$data['task_id']])->decrement('num',$num);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored('接单安排成功');
		}else{
			return $this->failed($message);
		}
	}

//	任务中止列表
	public function level_list(Request $request){
		try{
			$data=$this->leave->listStore($this->store_id);
			foreach($data as $kk=>$vv){
				$data[$kk]['created']=date('Y-m-d',strtotime($vv['created_at']));
			}
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}

//  任务中止详情
	public function level_desc(Request $request){
		$id=$request->id;
		try{
			$data=$this->leave->desc($id);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}
//	审核任务中止
	public function approve_level(Request $request){
		$id=$request->id;
		$status=$request->status;
		try{
			$result=$this->leave->where(['id'=>$id])->update(['status'=>$status]);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}



//	绑定审核流程
	public function binding($id){
		$workflowData=$this->Workflow->where(['company_id'=>$this->company_id,'style'=>3,'status'=>1])
			->first();
		$group_id=$this->belongsToGroup(self::$userInfo->department);
//		查询该用户的审批流
		$workflow_id=$this->Workflow_group->getworkflow_id($group_id,3);
		$workflow_desc['lovel_uid']=$this->Role->Role_user($workflowData->one);
		$workflow_desc['code']=Uuid::uuid4();
		foreach($workflow_desc['lovel_uid'] as $kk=>$vv){
			$data[$kk]['workflow_id']=$workflow_id['id'];
			$data[$kk]['from_uid']=self::$uid;
			$data[$kk]['lovel_role']=$workflowData->one;
			$data[$kk]['lovel_uid']=$vv;
			$data[$kk]['move_task_id']=$id;
			$data[$kk]['code']=$workflow_desc['code'];
			$data[$kk]['type']=3;
			$data[$kk]['created_at']=date("Y-m-d H:i:s",time());
		}
		DB::table('workflow_desc')->insert($data);
	}

}
